$(document).ready(function () {


    layui.use(function () {

        var layer = layui.layer;

        var laydate = layui.laydate;
        form = layui.form;

        form.on('select(searchProject)', function (data) {



            if (data.value != '') {
                $('#project-name').html($('.layui-select-title>input').val() + '  项目统计')
                queryTable()
            }
        });


        laydate.render({
            elem: "#tool-customize",
            range: true
            , done: function (value, date, endDate) {
                queryParams = {
                    "type": 3,
                    "beginTime": value.split(' - ')[0],
                    "endTime": value.split(' - ')[1]
                }
                $('#tool-year').html('年度')
                $('#tool-month').html('月度')
                queryTable()

            }
        });
        laydate.render({
            elem: "#tool-year",
            type: 'year'
            , value: new Date()
            , done: function (value, date, endDate) {
                queryParams = {
                    "type": 2,
                    "year": value
                }
                $('#tool-customize').html('自定义')
                $('#tool-month').html('月度')
                queryTable()
            }
        });
        laydate.render({
            elem: "#tool-month",
            type: 'month'
            , done: function (value, date, endDate) {
                queryParams = {
                    "type": 1,
                    "year": value.split('-')[0],
                    "month": value.split('-')[1]
                }
                $('#tool-customize').html('自定义')
                $('#tool-year').html('年度')
                queryTable()
            }
        });

        queryParams = {
            "type": 2,
            "year": $("#tool-year").html()
        }


        // 表格
        form = layui.form;
        layui.laydate.render({
            elem: "#input-date",
            range: true,
        });


        $('.contain-tab-item').eq(0).click()

        initSubMenu();

    });
})
var queryParams = {}
var params = {};//查询条件参数
var form, unit;
var drawer;//抽屉实例
var curType = 1;//当前选中考核标准ID


$('.tool-switch').click(function (e) {
    $(e.currentTarget).addClass('active').siblings().removeClass('active');

    if ($(e.currentTarget).attr('id') == "tool-today") {

        $('#tool-customize').html('自定义')
        $('#tool-year').html('年度')
        $('#tool-month').html('月度')

        queryParams = {
            "type": 5,
            "year": new Date().getFullYear(),
            "month": new Date().getMonth() + 1,
            "day": new Date().getDate()
        }
        queryTable()
        $('#executeData').show()
    } else {
        $('#executeData').hide()
    }


})

function executeData() {
    new AjaxChain()
        .setUrl('/train/score/count/today/execute')
        .enableLayer('数据同步成功',
            function () {
            },300)
        .then(data => {
            queryTable()

        })
        .catch(err => {
            console.error('请求异常:', err);
        })
        .send();

}


// 查询项目
queryPro = (type) => {

    new ST().trainProject(null, null, null, type).then(data => {
        $('#search-projectId').empty().append(data)

        form.render();


        $('#search-projectId').siblings("div.layui-form-select").find("dl").find('dd')[1].click();
        form.render('select'); // 更新下拉框

    })

}



// 顶部菜单切换
$('.contain-tab-item').click(function (e) {
    $(e.currentTarget).addClass('active').siblings().removeClass('active');
    curType = $(e.currentTarget).data().type;

    queryPro(curType)
})


//查询考核标准类型列表
queryTable = () => {
    queryParams.projectId = $('#search-projectId').val()
    queryParams.trainType = $('.contain-tab-item.active').data().type;


    layui.use('table', function () {
        var table = layui.table;

        params.status = curType

        //展示已知数据
        table.render({
            elem: '#table'
            , text: {
                none: '暂无数据'
            }
            , method: 'post'
            , url: ajaxPrefix() + "/train/score/ranking/list"
            , headers: {
                SessionId: JSON.parse(localStorage.getItem('sessionId'))
            }
            , contentType: 'application/json'
            , where:
                queryParams

            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status == 200 ? 0 : 1, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "data": res.result //解析数据列表
                };
            }
            , cols: [[
                {
                    field: 'photoPath', title: '照片', align: "center"
                    , templet: function (d) {

                        if (d.photoPath) {
                            return `<img title="查看" onclick=showPhoto("${d.photoPath}") src="/images/${d.photoPath}"
                              style="width: 0.4rem;height: 0.4rem;cursor: pointer;">`
                        } else {
                            return `<img title="暂未上传" onclick=showPhoto() src="../static/img/photo.png"
                             style="width: 0.4rem;height: 0.4rem;cursor: pointer;">`
                        }
                    },
                },
                { field: 'personName', title: '训练人', align: "center" },
                { field: 'ranking', title: '项目最佳排名', align: "center" },
                { field: 'minTime', title: '最好成绩', align: "center" },
                { field: 'avgTime', title: '平均成绩', align: "center" },
                { field: 'passRate', title: '合格率', align: "center" },
                { field: 'total', title: '训练总数(次)', align: "center" },
                { field: 'excellent', title: '优秀(次)', align: "center" },
                { field: 'good', title: '良好(次)', align: "center" },
                { field: 'pass', title: '合格(次)', align: "center" },
                { field: 'fail', title: '不合格(次)', align: "center" },
            ]]
            , height: '#contain-table-list-10'
            // ,skin: 'line' //表格风格
            , even: true
            , done: function (res, curr, count) {

                $('.layui-table-cell').height($('.contain-table-list').height() / 20);
            },

        });

    });
}

// 导出
function exportData() {

    var exportP = queryParams;
    exportP.sessionId = JSON.parse(localStorage.getItem('sessionId'))

    const params = new URLSearchParams(exportP);
    window.location.href = `/rest/fstation/train/score/ranking/export?${params}`;

}
